home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mint110s / console.c < prev    next >
C/C++ Source or Header  |  1994-02-11  |  5KB  |  310 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith.
  3. Copyright 1993,1994 Atari Corporation.
  4. All rights reserved.
  5. */
  6.  
  7. /* MiNT routines for doing console I/O */
  8.  
  9. #include "mint.h"
  10.  
  11. /*
  12.  * These routines are what Cconout, Cauxout, etc. ultimately call.
  13.  * They take an integer argument which is the user's file handle,
  14.  * and do the translation to a file ptr (and return an appropriate error
  15.  * message if necessary.
  16.  * "mode" may be RAW, COOKED, ECHO, or COOKED|ECHO.
  17.  * note that the user may not call them directly!
  18.  */
  19.  
  20. long
  21. file_instat(f)
  22.     FILEPTR *f;
  23. {
  24.     long r;
  25.  
  26.     if (!f) {
  27.         return EIHNDL;
  28.     }
  29.     r = 1;        /* default is to assume input waiting (e.g. TOS files)*/
  30.     (void)(*f->dev->ioctl)(f, FIONREAD, &r);
  31.     return r;
  32. }
  33.  
  34. long
  35. file_outstat(f)
  36.     FILEPTR *f;
  37. {
  38.     long r;
  39.  
  40.     if (!f) {
  41.         return EIHNDL;
  42.     }
  43.     r = 1;        /* default is to assume output OK (e.g. TOS files) */
  44.     (void)(*f->dev->ioctl)(f, FIONWRITE, &r);
  45.     return r;
  46. }
  47.  
  48. long
  49. file_getchar(f, mode)
  50.     FILEPTR *f;
  51.     int mode;
  52. {
  53.     char c;
  54.     long r;
  55.  
  56.     if (!f) {
  57.         return EIHNDL;
  58.     }
  59.     if (is_terminal(f)) {
  60.         return tty_getchar(f, mode);
  61.     }
  62.     r = (*f->dev->read)(f, &c, 1L);
  63.     if (r != 1)
  64.         return MiNTEOF;
  65.     else
  66.         return ((long)c) & 0xff;
  67. }
  68.  
  69. long
  70. file_putchar(f, c, mode)
  71.     FILEPTR *f;
  72.     long c;
  73.     int mode;
  74. {
  75.     char ch;
  76.  
  77.     if (!f) {
  78.         return EIHNDL;
  79.     }
  80.     if (is_terminal(f)) {
  81.         return tty_putchar(f, c & 0x7fffffffL, mode);
  82.     }
  83.     ch = c & 0x00ff;
  84.     return (*f->dev->write)(f, &ch, 1L);
  85. }
  86.  
  87. /*
  88.  * OK, here are the GEMDOS console I/O routines
  89.  */
  90.  
  91. long ARGS_ON_STACK
  92. c_conin()
  93. {
  94.     return file_getchar(curproc->handle[0], COOKED|ECHO);
  95. }
  96.  
  97. long ARGS_ON_STACK
  98. c_conout(c)
  99.     int c;
  100. {
  101.     return file_putchar(curproc->handle[1], (long)c, COOKED);
  102. }
  103.  
  104. long ARGS_ON_STACK
  105. c_auxin()
  106. {
  107.     return file_getchar(curproc->handle[2], RAW);
  108. }
  109.  
  110. long ARGS_ON_STACK
  111. c_auxout(c)
  112.     int c;
  113. {
  114.     return file_putchar(curproc->handle[2], (long)c, RAW);
  115. }
  116.  
  117. long ARGS_ON_STACK
  118. c_prnout(c)
  119.     int c;
  120. {
  121.     return file_putchar(curproc->handle[3], (long)c, RAW);
  122. }
  123.  
  124. long ARGS_ON_STACK
  125. c_rawio(c)
  126.     int c;
  127. {
  128.     long r;
  129.     PROC *p = curproc;
  130.  
  131.     if (c == 0x00ff) {
  132.         if (!file_instat(p->handle[0]))
  133.             return 0;
  134.         r = file_getchar(p->handle[0], RAW);
  135.         if (r <= 0)
  136.             return 0;
  137.         return r;
  138.     }
  139.     else
  140.         return file_putchar(p->handle[1], (long)c, RAW);
  141. }
  142.  
  143. long ARGS_ON_STACK
  144. c_rawcin()
  145. {
  146.     return file_getchar(curproc->handle[0], RAW);
  147. }
  148.  
  149. long ARGS_ON_STACK
  150. c_necin()
  151. {
  152.     return file_getchar(curproc->handle[0],COOKED|NOECHO);
  153. }
  154.  
  155. long ARGS_ON_STACK
  156. c_conws(str)
  157.     const char *str;
  158. {
  159.     const char *p = str;
  160.     long cnt = 0;
  161.  
  162.     while (*p++) cnt++;
  163.     return f_write(1, cnt, str);
  164. }
  165.  
  166. long ARGS_ON_STACK
  167. c_conrs(buf)
  168.     char *buf;
  169. {
  170.     long size, r;
  171.     char *s;
  172.  
  173.     size = ((long)*buf) & 0xff;
  174.     r = f_read(0, size, buf+2);
  175.     if (r < 0) {
  176.         buf[1] = 0;
  177.         return r;
  178.     }
  179. /* if reading from a file, stop at first CR or LF encountered */
  180.     s = buf+2;
  181.     size = 0;
  182.     while(r-- > 0) {
  183.         if (*s == '\r' || *s == '\n')
  184.             break;
  185.         s++; size++;
  186.     }
  187.     buf[1] = (char)size;
  188.     return 0;
  189. }
  190.  
  191. long ARGS_ON_STACK
  192. c_conis()
  193. {
  194.     return -(!!file_instat(curproc->handle[0]));
  195. }
  196.  
  197. long ARGS_ON_STACK
  198. c_conos()
  199. {
  200.     return -(!!file_outstat(curproc->handle[1]));
  201. }
  202.  
  203. long ARGS_ON_STACK
  204. c_prnos()
  205. {
  206.     return -(!!file_outstat(curproc->handle[3]));
  207. }
  208.  
  209. long ARGS_ON_STACK
  210. c_auxis()
  211. {
  212.     return -(!!file_instat(curproc->handle[2]));
  213. }
  214.  
  215. long ARGS_ON_STACK
  216. c_auxos()
  217. {
  218.     return -(!!file_outstat(curproc->handle[2]));
  219. }
  220.  
  221. /* Extended GEMDOS routines */
  222.  
  223. long ARGS_ON_STACK
  224. f_instat(h)
  225.     int h;
  226. {
  227.     PROC *proc;
  228.     int fh = h;
  229.  
  230. #if O_GLOBAL
  231.     if (fh >= 100) {
  232.         proc = rootproc;
  233.         fh -= 100;
  234.     } else
  235. #endif
  236.         proc = curproc;
  237.  
  238.     if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
  239.         DEBUG(("Finstat: bad handle %d", h));
  240.         return EIHNDL;
  241.     }
  242.     return file_instat(proc->handle[fh]);
  243. }
  244.  
  245. long ARGS_ON_STACK
  246. f_outstat(h)
  247.     int h;
  248. {
  249.     int fh = h;
  250.     PROC *proc;
  251. #if O_GLOBAL
  252.     if (fh >= 100) {
  253.         fh -= 100;
  254.         proc = rootproc;
  255.     } else
  256. #endif
  257.         proc = curproc;
  258.  
  259.     if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
  260.         DEBUG(("Foutstat: bad handle %d", h));
  261.         return EIHNDL;
  262.     }
  263.     return file_outstat(proc->handle[fh]);
  264. }
  265.  
  266. long ARGS_ON_STACK
  267. f_getchar(h, mode)
  268.     int h, mode;
  269. {
  270.     int fh = h;
  271.     PROC *proc;
  272.  
  273. #if O_GLOBAL
  274.     if (fh >= 100) {
  275.         fh -= 100;
  276.         proc = rootproc;
  277.     } else
  278. #endif
  279.         proc = curproc;
  280.     if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
  281.         DEBUG(("Fgetchar: bad handle %d", h));
  282.         return EIHNDL;
  283.     }
  284.     return file_getchar(proc->handle[fh], mode);
  285. }
  286.  
  287. long ARGS_ON_STACK
  288. f_putchar(h, c, mode)
  289.     int h;
  290.     long c;
  291.     int mode;
  292. {
  293.     int fh = h;
  294.     PROC *proc;
  295.  
  296. #if O_GLOBAL
  297.     if (fh >= 100) {
  298.         fh -= 100;
  299.         proc = rootproc;
  300.     } else
  301. #endif
  302.         proc = curproc;
  303.  
  304.     if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
  305.         DEBUG(("Fputchar: bad handle %d", h));
  306.         return EIHNDL;
  307.     }
  308.     return file_putchar(proc->handle[fh], c, mode);
  309. }
  310.